package hu.plajko.client;

import hu.plajko.shared.rpc.NotificationRPC;

import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.smartgwt.client.types.DragAppearance;
import com.smartgwt.client.types.Overflow;
import com.smartgwt.client.types.VerticalAlignment;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.util.ValueCallback;
import com.smartgwt.client.widgets.Button;
import com.smartgwt.client.widgets.Label;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.events.DragRepositionStartEvent;
import com.smartgwt.client.widgets.events.DragRepositionStartHandler;
import com.smartgwt.client.widgets.events.DragRepositionStopEvent;
import com.smartgwt.client.widgets.events.DragRepositionStopHandler;
import com.smartgwt.client.widgets.events.DragResizeStartEvent;
import com.smartgwt.client.widgets.events.DragResizeStartHandler;
import com.smartgwt.client.widgets.events.DragResizeStopEvent;
import com.smartgwt.client.widgets.events.DragResizeStopHandler;
import com.smartgwt.client.widgets.layout.VLayout;

public class NotificationPopup extends VLayout {

	private String id;

	private Label label = new Label();

	public NotificationPopup(String text) {
		this();
		label.setContents(text);
	}

	public NotificationPopup(NotificationRPC n) {
		this();
		updatePersistentData(n);
	}

	private boolean disableRefresh = false;


	
	public NotificationPopup() {
		
		
		setOverflow(Overflow.VISIBLE);
		setAutoHeight();
		setAutoWidth();
		setDragAppearance(DragAppearance.TARGET);
		setEdgeImage("border.png");
		setEdgeSize(18);
		setShowEdges(true);
		setEdgeShowCenter(true);
		setCanDragReposition(true);
		setCanDragResize(true);

		Button changeText = new Button("T");
		changeText.setWidth(18);
		changeText.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				SC.askforValue("Notification", "new text: ", new ValueCallback() {

					@Override
					public void execute(String value) {
						label.setContents(value);
						persist();

					}
				});

			}
		});

		final Button remove = new Button("X");

		remove.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {

				GreetingService.Client.getInstance().removeNotification(getPersistentData(), new AsyncCallback<Void>() {

					@Override
					public void onFailure(Throwable arg0) {
						SC.say(arg0.getMessage());

					}

					@Override
					public void onSuccess(Void arg0) {
						NotificationPopup.this.markForDestroy();

					}
				});

			}
		});

		remove.setWidth(18);
		ClickThroughLayout l = new ClickThroughLayout(ClickThroughLayout.LayoutPosition.TR, remove);
		addChild(l);
		
		ClickThroughLayout l2 = new ClickThroughLayout(ClickThroughLayout.LayoutPosition.C, changeText);
		l2.setParentElement(this);

		l2.show();

		label.setCanSelectText(true);
		label.setHeight100();
		label.setWidth100();
		label.setOverflow(Overflow.AUTO);
		label.setValign(VerticalAlignment.TOP);
		addMember(label);

		addDragRepositionStartHandler(new DragRepositionStartHandler() {

			@Override
			public void onDragRepositionStart(DragRepositionStartEvent event) {
				disableRefresh = true;

			}
		});

		addDragResizeStartHandler(new DragResizeStartHandler() {

			@Override
			public void onDragResizeStart(DragResizeStartEvent event) {
				disableRefresh = true;

			}
		});

		addDragRepositionStopHandler(new DragRepositionStopHandler() {

			@Override
			public void onDragRepositionStop(DragRepositionStopEvent event) {
				persist();
				disableRefresh = false;
			}
		});

		addDragResizeStopHandler(new DragResizeStopHandler() {

			@Override
			public void onDragResizeStop(DragResizeStopEvent event) {
				persist();
				disableRefresh = false;
			}
		});

		new Timer() {

			@Override
			public void run() {
				if (!disableRefresh)
					GreetingService.Client.getInstance().getData(NotificationPopup.this.id, new AsyncCallback<NotificationRPC>() {

						@Override
						public void onFailure(Throwable arg0) {
							SC.say(arg0.getMessage());
						}

						@Override
						public void onSuccess(NotificationRPC arg0) {
							updatePersistentData(arg0);

						}
					});

			}
		}.scheduleRepeating(2000);
	}

	void persist() {
		disableRefresh = true;
		System.out.println("perist notification");
		GreetingService.Client.getInstance().updateNotification(getPersistentData(), new AsyncCallback<String>() {

			@Override
			public void onSuccess(String result) {
				NotificationPopup.this.id = result;
				disableRefresh = false;
			}

			@Override
			public void onFailure(Throwable caught) {
				disableRefresh = true;
			}
		});
	}

	public void updatePersistentData(NotificationRPC n) {
		System.out.println("w:" + n.getWidth() + ",h:" + n.getHeight());
		label.setContents(n.getText());
		setTop(n.getTop());
		setLeft(n.getLeft());
		setWidth(n.getWidth() < 1 ? 1 : n.getWidth());
		setHeight(n.getHeight() < 1 ? 1 : n.getHeight());
		setZIndex(n.getZindex());
		this.id = n.getId();
	}

	public NotificationRPC getPersistentData() {
		NotificationRPC n = new NotificationRPC();
		n.setId(this.id);
		n.setText(label.getContents());
		n.setLeft(getAbsoluteLeft());
		n.setTop(getAbsoluteTop());
		n.setWidth(getVisibleWidth());
		n.setHeight(getVisibleHeight());
		n.setZindex(getZIndex());
		return n;
	}

}
